package com.achievement.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.achievement.domain.dto.ProjectProductDTO;
import com.achievement.domain.dto.UpdateProjectProductDTO;
import com.achievement.domain.entity.ProjectProductEntity;
import com.achievement.domain.vo.ProjectProductVO;
import com.achievement.mapper.ProjectProductMapper;
import com.achievement.mappings.ProjectProductMapping;
import com.achievement.service.IProjectProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author CNKI
* @description 针对表【biz_project_product(成果依托项目列表)】的数据库操作Service实现
* @createDate 2025-02-17 16:19:55
*/
@Service
public class ProjectProductServiceImpl extends ServiceImpl<ProjectProductMapper, ProjectProductEntity>
    implements IProjectProductService {

    @Autowired
    private ProjectProductMapping mapping;


    /**
     * 新增或者编辑保存成果依托项目关系
     *
     * @param dto ProjectProductDTO
     * @return boolean
     */
    @Override
    public boolean save(UpdateProjectProductDTO dto) {

        //先删除之前的依托项目关系
        LambdaQueryWrapper<ProjectProductEntity> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectProductEntity::getProductId,dto.getProductId());
        this.remove(queryWrapper);

        if (CollUtil.isEmpty(dto.getProductDTOList())){
            return true;
        }


        //再新增依托项目关系
        List<ProjectProductEntity> entityList = new ArrayList<>();

        for (ProjectProductDTO paperProjectProductDTO : dto.getProductDTOList()) {
            String UUID = IdUtil.randomUUID().replace("-", "");

            paperProjectProductDTO.setId(UUID);

            entityList.add(mapping.dto2Entity(paperProjectProductDTO));
        }


        return this.saveBatch(entityList);
    }

    /**
     * 根据成果id，查询依托项目关系列表
     */
    @Override
    public List<ProjectProductVO> selectByProductId(String productId) {
        LambdaQueryWrapper<ProjectProductEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectProductEntity::getProductId, productId);
        List<ProjectProductEntity> list = this.list(queryWrapper);
        List<ProjectProductVO> voList = list.stream().map(mapping::entity2Vo).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<ProjectProductVO> selectByProjectId(String projectId) {
        LambdaQueryWrapper<ProjectProductEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectProductEntity::getProjectId,projectId);
        List<ProjectProductEntity> list = this.list(queryWrapper);
        List<ProjectProductVO> voList = list.stream().map(mapping::entity2Vo).collect(Collectors.toList());
        return voList;
    }
}




